#ifndef __COMPONENT_HERD_BEHAVIOR_H__
#define __COMPONENT_HERD_BEHAVIOR_H__

#include "ComponentBehavior.hpp"
#include "Engine/MathUtils.hpp"
#include "Engine/Vector2.hpp"

#include "EasyMachine.hpp"

using namespace PocketSurvival::Engine;

namespace PocketSurvival
{

    struct ComponentHerdBehavior;
    struct ComponentCreature;
    struct ComponentPathfinding;

    class SubsystemTimeOfDay;
    class SubsystemAnimalEntities;

    // 兽群行为

    struct ComponentHerdBehavior : public ComponentBehavior
    {
    protected:
        enum class MachineState
        {
            Inactive,           // 不活跃的
            Stuck,
            Herd
        };

        class StateMachine : public EasyMachine<MachineState>
        {
        private:
            ComponentHerdBehavior * m_herdBehavior;
        public:
            StateMachine(ComponentHerdBehavior *herdBehavior);

            virtual void enter() override;
            virtual void update(float dt) override;
            virtual void leave() override;
        };

    protected:
        Vector2 m_look;
        float m_herdingRange;
        bool m_autoNearbyCreaturesHelp;
        std::string herdName = "";

        float m_importanceLevel = 1.0f;
        StateMachine *m_stateMachine;

    public:
        ComponentCreature *componentCreature;
        ComponentPathfinding *componentPathfinding;

        SubsystemTimeOfDay *subsystemTimeOfDay;
        SubsystemAnimalEntities *m_animalEntities;


        ComponentHerdBehavior() = default;
        ~ComponentHerdBehavior();

        virtual float getImportanceLevel() override;

        void callNearbyCreaturesHelp(entt::entity target, float maxRange, float maxChaseTime, bool isPersistent);
        Vector3 findHerdCenter();

        virtual void update(float dt) override;
        virtual void load(const nlohmann::json &jsonData) override;
        virtual const std::string &getName() const override;
        static const std::string &GetName();
    };
}

#endif
